package com.dpiinc.TechPage.user;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.dpiinc.TechPage.BaseActivity;
import com.dpiinc.TechPage.R;
import com.dpiinc.TechPage.adapter.CountryAdapter;
import com.dpiinc.TechPage.model.jbean.CountryBean;
import com.github.promeg.pinyinhelper.Pinyin;
import com.github.promeg.tinypinyin.lexicons.android.cncity.CnCityDict;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import me.yokeyword.indexablerv.IndexableAdapter;
import me.yokeyword.indexablerv.IndexableLayout;
import me.yokeyword.indexablerv.SimpleHeaderAdapter;

public class CountryPickerActivity extends BaseActivity {

    private CountryAdapter mAdapter;
    private IndexableLayout indexableLayout;
    private SimpleHeaderAdapter commonCountryAdapter;
    private Intent intent;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        setContentView(R.layout.country_pick);
        super.onCreate(savedInstanceState);

        setBackWhite();
        setstatusBarColor(R.color.color_00bad2);

        intent = getIntent();
        indexableLayout = findViewById(R.id.indexableLayout);
        indexableLayout.setLayoutManager(new LinearLayoutManager(this));

        mAdapter = new CountryAdapter(this);
        indexableLayout.setAdapter(mAdapter);
        indexableLayout.setOverlayStyle_Center();

        mAdapter.setDatas(initDatas());

        Pinyin.init(Pinyin.newConfig().with(CnCityDict.getInstance(this)));
        indexableLayout.setCompareMode(IndexableLayout.MODE_ALL_LETTERS);

        commonCountryAdapter = new SimpleHeaderAdapter<>(mAdapter, null, getString(R.string.common_region), initCommonCountryDatas());
        indexableLayout.addHeaderAdapter(commonCountryAdapter);

        onListen();
    }

    /**
     *
     * 初始化数据
     * @return
     */
    private List<CountryBean> initDatas(){
        List<CountryBean> list = new ArrayList<>();
        List<String> countryStrings = Arrays.asList(getResources().getStringArray(R.array.CountryCodes));
        for(String item : countryStrings){
            CountryBean countryBean = new CountryBean();
            countryBean.setName(item.split(",")[1]);
            countryBean.setNumber(item.split(",")[0]);
            list.add(countryBean);
        }
        return list;
    }

    private List<CountryBean> initCommonCountryDatas(){
        List<CountryBean> list = new ArrayList<>();
        List<String> countryStrings = Arrays.asList(getResources().getStringArray(R.array.CommonCountry));
        for(String item : countryStrings){
            CountryBean countryBean = new CountryBean();
            countryBean.setName(item.split(",")[1]);
            countryBean.setNumber(item.split(",")[0]);
            list.add(countryBean);
        }
        return list;
    }

    public void onListen(){
        mAdapter.setOnItemContentClickListener(new IndexableAdapter.OnItemContentClickListener<CountryBean>() {
            @Override
            public void onItemClick(View v, int originalPosition, int currentPosition, CountryBean entity) {
                intent.putExtra("country", entity);
                setResult(RESULT_OK, intent);
                finish();
            }
        });
    }
}
